package com.shenhan.datasource.config;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * @author shenhan
 * @create 2020/9/8 11:51
 * @description 自定义分区器 指定分区规则 默认key的hash
 */
public class CustomerPartitioner implements Partitioner {
    //根据参数按照指定的规则进行分区
    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        Integer k = (Integer) key;
        Integer number = cluster.partitionCountForTopic(topic);
        return k % number;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> map) {

    }
}
